package com.situ.wms.dao;

import java.util.ArrayList;
import java.util.List;

import com.situ.wms.entity.Goods;
import com.situ.wms.entity.Manager;
import com.situ.wms.util.DBUtil;

public class UserDao {

	public List<Manager> select(Integer page, Integer limit, String keyword) {
		
		String sql;
		if(keyword==null||keyword.equals("")) {
		sql="select * from manager  limit ? ,? ";}
		else {
			sql="select * from manager where instr(name,'"+keyword+"')>0 limit ? , ? ";
		}
		return DBUtil.executeDQL(sql, Manager.class,(page-1)*limit ,limit);
		
	}
	public List<Manager> selectByUsername(String username){
		String sql="select * from manager where username = ?";
		return DBUtil.executeDQL(sql, Manager.class, username);
	}

	public Long count(String keyword) {
		String sql;
		if(keyword==null||keyword.equals("")) {
			sql="select count(1) from manager";
			}
			else {
				
				sql="select count(1) from manager where instr(name,'"+keyword+"')>0 ";
			}
		
		return DBUtil.executeCount(sql);
	}

	public void delete(Integer id) {
		// TODO Auto-generated method stub
		String sql;
		sql="delete  from manager where id = ?";
		DBUtil.executeDML(sql,id);
		
	}
	

	public void add(Manager emp) {
		// TODO Auto-generated method stub
		String sql="insert into manager (name,username,password) values(?,?,?) ";
		String name=emp.getName();
		String username=emp.getUsername();
		String password=emp.getPassword();
		DBUtil.executeDML(sql,name,username,password);
	}

	public  void update(Manager emp) {
		// TODO Auto-generated method stub
		String sql ="update manager set name =? ,  username=? , password=? where id =?";
	 	DBUtil.executeDML(sql,emp.getName(),emp.getUsername(),emp.getPassword(),emp.getId());
	}
	public Manager selectById(int id) {
	
		String sql;
		sql="select *  from manager where id = ?";
		List<Manager>list = new ArrayList<>();
		list=DBUtil.executeDQL(sql, Manager.class, id);
		if(list.size()>0) {
			return list.get(0);
		}else {return null;}
	}

    public Manager select(String username) {
		String sql;
		sql="select *  from manager where username = ?";
		List<Manager>list = new ArrayList<>();
		list=DBUtil.executeDQL(sql, Manager.class, username);
		if(list.size()>0) {
			return list.get(0);
		}else {return null;}
    }
}
